﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

// Struct that represents a (page number, url) association
public struct PageUrl
{
	private string page;
	private string url;

	// constructor
	public PageUrl(string page, string url)
	{
		this.page = page;
		this.url = url;
	}

	// page property
	public string Page
	{
		get
		{
			return page;
		}
	}

	// url property
	public string Url
	{
		get
		{
			return url;
		}
	}
}

public partial class UserControls_Pagination : System.Web.UI.UserControl
{
	// Show the pagination
	public void ShowPagination(int currentPage, int howManyPages, string firstPageUrl, string pageUrlFormat, bool displayPagination)
	{
		// Display paging controls
		if (howManyPages > 1)
		{
			// Make the pagination visible
			this.Visible = true;

			// Display the current page
			currentPageLabel.Text = currentPage.ToString();
			howManyPagesLabel.Text = howManyPages.ToString();

			// Create the Previous link
			if (currentPage == 1)
			{
				previousLink.Enabled = false;
			}
			else
			{
				previousLink.NavigateUrl = String.Format(pageUrlFormat, currentPage - 1);
			}

			// Create the Next link
			if (currentPage == howManyPages)
			{
				nextLink.Enabled = false;
			}
			else
			{
				nextLink.NavigateUrl = String.Format(pageUrlFormat, currentPage + 1);
			}

			// create the page links
			if (displayPagination)
			{
				// the list of pages and their URLs as an array
				PageUrl[] pages = new PageUrl[howManyPages];
				// generate (page, url) elements
				pages[0] = new PageUrl("1", firstPageUrl);
				for (int i = 2; i <= howManyPages; i++)
				{
					pages[i - 1] =
					  new PageUrl(i.ToString(), String.Format(pageUrlFormat, i));
				}
				// do not generate a link for the current page
				pages[currentPage - 1] = new PageUrl((currentPage).ToString(), "");
				// feed the pages to the repeater
				PagesRepeater.DataSource = pages;
				PagesRepeater.DataBind();
			}
		}
	}
}